import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * 注销用户功能
 * SQL语句:DELETE FROM userinfo WHERE username=?
 */
public class JDBCDemo3 {
    public static void main(String[] args) {
        /*
            1.接收用户输入;
            2.加载数据库驱动;
            3.创建数据库连接对象;
            4.创建执行对象;
            5.执行SQL;
            6.断开数据库连接;
         */
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String username = scanner.nextLine();

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection connection = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
                    "root",
                    "root"
            );
            //3.创建执行对象: .var+回车+回车
            Statement statement = connection.createStatement();
            String sql = "DELETE FROM userinfo WHERE username='"+username+"'";
            int i = statement.executeUpdate(sql);
            if (i>0){
                System.out.println("注销用户成功");
            }else{
                System.out.println("注销用户失败");
            }
            connection.close();
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }
}
















